<template>
  <div class="otherOrder">
    <van-sticky z-index="9">
      <div class="pub_tab">
        <van-tabs
          v-model:active="activeTab"
          animated
          sticky
          :line-height="8"
          title-active-color="#12142E"
          title-inactive-color="#454757"
          color="#FF7A1F"
          background="#f7f7f7"
          @click-tab="onClickTab"
        >
          <van-tab
            :title="`${item.title}`"
            v-for="(item, index) in tabbar"
            :key="index"
          />
        </van-tabs>
      </div>
    </van-sticky>
    <div class="content_box">
      <van-pull-refresh
        disabled
        v-model="refreshing"
        @refresh="onRefresh"
      >
        <van-list
          v-model:loading="loading"
          :finished="finished"
          @load="loadMore"
          :immediate-check="false"
        >
          <div class="order">
            <div
              class="orderBox"
              v-for="(item, index) in listData"
              :key="index"
            >
              <div class="orderInfo">
                <div class="orderInfoBox">
                  <p
                    class="line25 title van-multi-ellipsis--l2"
                    v-if="item.status == 1"
                  >
                    <van-tag
                      style="height: 19px; width: 55px; justify-content: center"
                      class="tagleft"
                      color="#EE762A"
                      type="primary"
                    >
                      进行中
                    </van-tag>
                    <span
                      class="content3 fontBold color3 "
                      v-if="item.pay_type == 1 && item.is_apply == 0"
                    >会员续费 / {{ item.company_level }}</span>
                    <span
                      class="content3 fontBold color3"
                      v-if="item.pay_type == 2"
                    >活动报名 / {{ item.title }}</span>
                    <span
                      class="content3 fontBold color3"
                      v-if="item.pay_type == 3"
                    >课程报名 / {{ item.title }}</span>
                    <span
                      class="content3 fontBold color3"
                      v-if="item.pay_type == 1 && item.is_apply == 1"
                    >企业入会 / {{ item.company_level || item.title }}</span>
                  </p>
                  <p
                    class="line25 title van-multi-ellipsis--l2"
                    v-if="item.status == 2"
                  >
                    <van-tag
                      style="height: 19px; width: 55px; justify-content: center"
                      class="tagleft"
                      color="#85898C"
                      type="primary"
                    >
                      已完成
                    </van-tag>
                    <span
                      class="content3 fontBold color3"
                      v-if="item.pay_type == 1 && item.is_apply == 0"
                    >会员续费 / {{ item.company_level }}</span>
                    <span
                      class="content3 fontBold color3"
                      v-if="item.pay_type == 2"
                    >活动报名 / {{ item.title }}</span>
                    <span
                      class="content3 fontBold color3"
                      v-if="item.pay_type == 3"
                    >课程报名 / {{ item.title }}</span>
                    <span
                      class="content3 fontBold color3"
                      v-if="item.pay_type == 1 && item.is_apply == 1"
                    >企业入会 / {{ item.company_level }}</span>
                  </p>
                  <p
                    class="line25 title van-multi-ellipsis--l2"
                    v-if="item.status == 3"
                  >
                    <van-tag
                      style="height: 19px; width: 55px; justify-content: center"
                      class="tagleft"
                      color="#F44A3D"
                      type="primary"
                    >
                      已失效
                    </van-tag>
                    <span
                      class="content3 fontBold color3"
                      v-if="item.pay_type == 1 && item.is_apply == 0"
                    >会员续费 / {{ item.company_level }}</span>
                    <span
                      class="content3 fontBold color3"
                      v-if="item.pay_type == 2"
                    >活动报名 / {{ item.title }}</span>
                    <span
                      class="content3 fontBold color3"
                      v-if="item.pay_type == 3"
                    >课程报名 / {{ item.title }}</span>
                    <span
                      class="content3 fontBold color3"
                      v-if="item.pay_type == 1 && item.is_apply == 1"
                    >企业入会 / {{ item.company_level }}</span>
                  </p>
                  <p class="moeny flex align_center justify_between">
                    <span class="content2 font400 color6">{{
                      item.create_time
                    }}</span>
                    <span
                      class="content3 font400 color1"
                    >￥{{ item.price }}</span>
                  </p>
                </div>
                <div class="confin flex align_center justify_between">
                  <div
                    class="confinItem cancle font400 content2 color3"
                    v-if="item.status == 1"
                    @click="cancle(item.id)"
                  >
                    取消订单
                  </div>
                  <div
                    class="confinItem see font400 content2 color2"
                    :class="{
                      confinItemW: item.status == 2 || item.status == 3,
                    }"
                    @click="see(item)"
                  >
                    查看订单
                  </div>
                </div>
              </div>
            </div>
          </div>
          <vant-list />
        </van-list>
      </van-pull-refresh>
    </div>
    <no-data v-if="listData.length === 0" />
  </div>
  <div class="fexd_footer">
    <div class="footer_btn flex align_center justify_between">
      <div class="flex align_center">
        <div
          @click="back"
          class="agree font400 content3 color4"
        >
          返回
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Toast, Dialog } from 'vant'
import { orderList, orderCancel } from '@/api'
import { ref, defineAsyncComponent, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'
const NoData = defineAsyncComponent(
  () => import('@/components/NoData/index.vue')
)
const router = useRouter()
const route = useRoute()
const store = useStore()
const activeTab: any = ref(Number(route.query.type) || 0)
const loading: any = ref(false)
const finished: any = ref(false)
const refreshing: any = ref(false)
const listData: any = ref([])
const tabbar: any = ref([
  {
    title: '全部'
  },
  {
    title: '进行中'
  },
  {
    title: '已完成'
  },
  {
    title: '已失效'
  }
])

const form: any = ref({
  page: 1,
  pageSize: 10
})

const selectedDemandTypeId = computed(() => {
  return Number(route.query.type) || 0
})

watch(selectedDemandTypeId, (newVal, oldVal) => {
  onRefresh()
})

const back = () => {
  if (store.state.from.path !== '/') {
    router.back()
  } else {
    router.replace('/')
  }
}

const cancle = async (id: any) => {
  Dialog.confirm({
    title: '温馨提示',
    message: '此操作将取消该订单, 是否继续?',
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(async () => {
    try {
      await orderCancel({ order_id: id })
      onRefresh()
      Toast('取消成功')
    } catch (error: any) {
      Toast(error.message)
    }
  })
}

const see = async (item: any) => {
  if (item.pay_method === 2) {
    router.replace({ path: '/applyOf', query: { type: 2, orderId: item.id, back: 'orderList' } })
  } else if (item.pay_method === 1) {
    router.push({
      path: '/applyOf/weChat',
      query: {
        orderId: item.id,
        pay_type: 2,
        paid_id: item.paid_id,
        pay_method: item.pay_method,
        user_ids: JSON.stringify(item.pay_id)
      }
    })
  }
}

const onClickTab = async (item: any) => {
  router.replace({
    path: '/otherServices/order',
    query: { type: item.name }
  })
  activeTab.value = Number(item.name)
}

const onRefresh = async () => {
  form.value.page = 1
  const list: any = await loadDemandList()
  listData.value = list
  refreshing.value = false
}

const loadMore = async () => {
  form.value.page = form.value.page + 1
  const list: any = await loadDemandList()
  listData.value = [...listData.value, ...list]
  loading.value = false
}

const loadDemandList = async () => {
  // 异步更新数据
  const res: any =
    (await orderList({ ...form.value, type: selectedDemandTypeId.value })) ||
    []
  if (!Array.isArray(res) && typeof res.list === 'object') {
    const keys: any = Object.keys(res.list)
    const newRes: any = keys.map((k: any) => res.list[k])
    finished.value = newRes.length < form.value.pageSize
    return newRes
  }

  finished.value = res.list.length < form.value.pageSize
  return res
}

onRefresh()
</script>
<style lang="less" scoped>
.otherOrder {
  padding: 15px 0 94px;
}
.order {
  width: 314px;
  margin: 20px auto 0;
  .orderBox {
    margin-bottom: 15px;
    .orderInfo {
      background: #ffffff;
      border-radius: 5px;
      .orderInfoBox {
        padding: 15px 15px 0;
        .title {
          // height: 40px;
        }
        .tagleft {
          margin-right: 5px;
        }
        .moeny {
          margin-top: 20px;
          margin-bottom: 15px;
        }
      }
    }
  }
  .confin {
    border-top: 1px solid #cacaca;
    .confinItem {
      width: 50%;
      text-align: center;
      height: 48px;
      line-height: 48px;
      border-right: 1px solid #cacaca;
    }
    .confinItemW {
      width: 100%;
    }
    .confinItem:last-child {
      border-right: 0;
    }
  }
}
.fexd_footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 70px;
  background-color: #fff;
  box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);
  .footer_btn {
    padding: 0 30px;
    height: 100%;
    text-align: center;
    .agree {
      width: 315px;
      height: 44px;
      background: #85898c;
      border-radius: 5px;
      text-align: center;
      line-height: 44px;
    }
  }
}
@media (min-width: 750px) {
  .otherOrder {
    padding: 15px 0 94px;
  }
  .order {
    width: 314px;
    margin: 20px auto 0;
    .orderBox {
      margin-bottom: 15px;
      .orderInfo {
        background: #ffffff;
        border-radius: 5px;
        .orderInfoBox {
          padding: 15px 15px 0;
          .title {
            height: 40px;
          }
          .tagleft {
            margin-right: 5px;
          }
          .moeny {
            margin-top: 20px;
            margin-bottom: 15px;
          }
        }
      }
    }
    .confin {
      border-top: 1px solid #cacaca;
      .confinItem {
        width: 50%;
        text-align: center;
        height: 48px;
        line-height: 48px;
        border-right: 1px solid #cacaca;
      }
      .confinItemW {
        width: 100%;
      }
      .confinItem:last-child {
        border-right: 0;
      }
    }
  }
  .fexd_footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);
    .footer_btn {
      padding: 0 30px;
      height: 100%;
      text-align: center;
      .agree {
        width: 315px;
        height: 44px;
        background: #85898c;
        border-radius: 5px;
        text-align: center;
        line-height: 44px;
      }
    }
  }
}
</style>
